<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <th:block th:include="include :: header('代码生成列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
  <div class="row">
    <div class="col-sm-12 search-collapse">
      <form id="gen-form">
        <div class="select-list">
          <ul>
            <li>
              表名称：<input type="text" name="tableName"/>
            </li>
            <li>
              表描述：<input type="text" name="tableComment"/>
            </li>
            <li>
              数据源：
              <select name="dataSourceId" th:with="data=${dataSourceList}">
                <option value="">所有</option>
                <option th:each="dataSource : ${data}"
                        th:text="${dataSource.name}" th:value="${dataSource.id}"></option>
              </select>
            </li>
            <li class="select-time">
              <label>创建时间：</label>
              <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="beginTime"/>
              <span>-</span>
              <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
            </li>
            <li>
              <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()">
                <i class="fa fa-search"></i>&nbsp;搜索
              </a>
              <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()">
                <i class="fa fa-refresh"></i>&nbsp;重置
              </a>
            </li>
          </ul>
        </div>
      </form>
    </div>

    <div class="btn-group-sm" id="toolbar" role="group">
      <a class="btn btn-success multiple disabled" onclick="batchGenCode()">
        <i class="fa fa-download"></i> 生成
      </a>
      <a class="btn btn-info" onclick="selectDataSource()">
        <i class="fa fa-upload"></i> 导入
      </a>
      <a class="btn btn-primary single disabled" onclick="$.operate.editTab()">
        <i class="fa fa-edit"></i> 修改
      </a>
      <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
        <i class="fa fa-remove"></i> 删除
      </a>
      <a class="btn btn-warning" onclick="showEditQuickConfig()">
        <i class="fa fa-edit"></i> 快速配置
      </a>
      <a class="btn btn-danger" onclick="resetCache()">
        <i class="fa fa-refresh"></i> 清理模版缓存
      </a>
    </div>

    <div class="col-sm-12 select-table table-striped">
      <table id="bootstrap-table"></table>
    </div>
  </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-export-js"/>
<script th:inline="javascript">
  var prefix = ctx + "tool/gen";

  $(function () {
    var options = {
      url: prefix + "/list",
      updateUrl: prefix + "/edit/{id}",
      removeUrl: prefix + "/remove",
      sortName: "createTime",
      sortOrder: "desc",
      showExport: true,
      modalName: "生成配置",
      rememberSelected: true,
      uniqueId: "tableId",
      columns: [
        {
          field: 'state',
          checkbox: true
        },
        {
          field: 'tableId',
          title: '编号',
          align: 'center',
          visible: false
        },
        {
          title: "序号",
          align: 'center',
          formatter: function (value, row, index) {
            return $.table.serialNumber(index);
          }
        },
        {
          field: 'tableName',
          title: '表名称',
          formatter: function (value, row, index) {
            return $.table.tooltip(value);
          }
        },
        {
          field: 'tableComment',
          title: '表描述',
          formatter: function (value, row, index) {
            return $.table.tooltip(value, 15);
          }
        },
        {
          field: 'className',
          title: '实体类名称',
          sortable: true
        },
        {
          field: 'createTime',
          title: '创建时间',
          sortable: true
        },
        {
          field: 'updateTime',
          title: '更新时间',
          sortable: true
        },
        {
          title: '操作',
          align: 'center',
          formatter: function (value, row, index) {
            var actions = [];
            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="preview(\'' + row.tableId + '\')"><i class="fa fa-search"></i>预览</a> ');
            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.tableId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.tableId + '\')"><i class="fa fa-remove"></i>删除</a> ');
            actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="synchDb(\'' + row.tableName + '\',\'' + row.tableId + '\')"><i class="fa fa-refresh"></i>同步</a> ');
            actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="genCode(\'' + row.tableName + '\',\'' + row.genType + '\')"><i class="fa fa-bug"></i>生成代码</a> ');
            return actions.join('');
          }
        }]
    };
    $.table.init(options);
  });

  // 预览代码
  function preview(tableId) {
    top.layer.open({
      type: 2,
      shift: 4,
      maxmin: true,
      title: "代码预览",
      shadeClose: true,
      shade: 0.5,
      area: ['90%', '90%'],
      content: prefix + "/preview/" + tableId
    });
  }

  // 生成代码
  function genCode(tableName, genType) {
    $.modal.confirm("确定要生成" + tableName + "表代码吗？", function () {
      if (genType === "0") {
        location.href = prefix + "/download/" + tableName;
        layer.msg('执行成功, 正在生成代码请稍后…', {icon: 1});
      } else if (genType === "1") {
        $.operate.get(prefix + "/genCode/" + tableName);
      }
    })
  }

  // 同步数据库
  function synchDb(tableName, tableId) {
    $.modal.confirm("确认要强制同步" + tableName + "表结构吗？", function () {
      $.operate.get(prefix + "/synchDb/" + tableName + "/" + tableId);
    })
  }

  // 批量生成代码
  function batchGenCode() {
    var rows = $.table.selectColumns("tableName");
    if (rows.length === 0) {
      $.modal.alertWarning("请选择要生成的数据");
      return;
    }
    $.modal.confirm("确认要生成选中的" + rows.length + "条数据吗?", function () {
      location.href = prefix + "/batchGenCode?tables=" + rows;
      layer.msg('执行成功,正在生成代码请稍后…', {icon: 1});
    });
  }

  // 导入表结构
  function importTable(dataSourceId) {
    var importTableUrl = prefix + "/importTable/" + dataSourceId;
    $.modal.open("导入表结构", importTableUrl);
  }

  // 选择数据源
  function selectDataSource() {
    var url = ctx + "system/dataSource/select";
    var options = {
      title: '选择数据源',
      url: url,
      callBack: selectDataSourceCallback
    };
    $.modal.openOptions(options);
  }

  /* 选择数据源回调 */
  function selectDataSourceCallback(index, layero) {
    // 得到iframe页的body内容
    // var body = layer.getChildFrame('body', index);
    var data = layero.find("iframe")[0].contentWindow.getSelections();
    if (data.length === 0) {
      $.modal.msgError("请选择数据源");
    } else {
      layer.close(index);
      importTable(data[0]);
    }
  }

  function showEditQuickConfig() {
    $.modal.open("快速配置", ctx + "system/config/edit/quick");
  }

  /** 清理模版缓存 */
  function resetCache() {
    $.operate.get(ctx + "tool/template/resetCache");
  }
</script>
</body>
</html>